<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		<meta name="viewport" id="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
		<meta name="format-detection" content="telephone=no" />
        <link rel="stylesheet" href="/static/webAPP/css/reset.css" type="text/css"/>
        <link rel="stylesheet" href="/static/webAPP/lib/layui-v2.5.6/layui/css/layui.css">
		<title>微信登录</title>
		<style>
			.weichatlogin{
                margin: 0 auto;
                
            }
            .weichatlogin img{
                position: absolute;
                top: 26%;
                left: 32%;
                width: 40%;
                padding-bottom: 40%;
            }
            .weichatlogin span{
                width: 100%;
                position: absolute;
                bottom: 20%;
                display:flex;
                flex-wrap:wrap;
                justify-content: space-between;
            }
            .weichatlogin span li{
                margin: 10px 10px;
                width: 100%;
                height: 36px;
                text-align: center;
                line-height: 36px;
                font-size: 22px;
                color: gray;
            }
            .layui-icon-loading{
                text-align: center;
                font-size: 36px;
                margin-left: 45%;
                color:gray;
            }
		</style>
	</head>
	<body>
		<div class="weichatlogin">
            <a href="javascript:history.go(-1);" class="loginTop"></a>
			<img src="/static/webAPP/images/icon_weichat.png" alt="">
			<span>
                <i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>
                <li>正在登录...</li>
            </span>
		</div>
        <script src="/static/webAPP/js/jquery-1.8.3.min.js"></script>
        <script src="/static/webAPP/lib/mobile/layer.js"></script>
        <script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js "></script>
        <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=DouIq9pM7lqTiVOnzOQ6Q8biwgi2GIM0&s=1">
            //v2.0版本的引用方式：src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"
            </script>
		<script>
			$(function(){
                var userAgent = navigator.userAgent.toLowerCase();

                
                var appId 
                var timestamp 
                var nonceStr 
                var signature
                var url = window.location.href;
                $.ajax({
                    url: '/Api/JSAPI/getJSConfig',
                    data: {url: url},
                    type: "get",
                    async : false,
                    dataType: "json",
                    success: function(mag){
                        appId = mag.data.appId
                        timestamp = mag.data.timestamp
                        nonceStr = mag.data.nonceStr
                        signature = mag.data.sign
                    }
                })
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: appId, // 必填，公众号的唯一标识
                    timestamp: timestamp, // 必填，生成签名的时间戳
                    nonceStr: nonceStr, // 必填，生成签名的随机串
                    signature: signature,// 必填，签名
                    jsApiList: ['getLocation'] // 必填，需要使用的JS接口列表
                });
                wx.error(function (res) {

                });
                wx.ready(function(){
                    wx.getLocation({
                        type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                        success: function (wxres) {
                            var latitude = wxres.latitude; // 纬度，浮点数，范围为90 ~ -90
                            var longitude = wxres.longitude; // 经度，浮点数，范围为180 ~ -180。
                            var speed = wxres.speed; // 速度，以米/每秒计
                            var accuracy = wxres.accuracy; // 位置精度

                            $.ajax({
                                url:'https://api.map.baidu.com/geocoder/v2/?ak=Ya2nSaqjT3vNrIgba1v4nfWzSxGdtgZD&location=' + latitude + ',' + longitude + '&output=json&pois=1&s=1',
                                dataType: "jsonp",
                                callback: 'BMap._rd._cbk43398',
                                success: function(res){
                                    var city = res.result.addressComponent.city
                                    var citycode = res.result.cityCode
                                    sessionStorage.setItem("citycode", citycode);
                                    sessionStorage.setItem("city", city);
                                    // window.location = "/static/webAPP/index.html";
                                    if(userAgent.match(/MicroMessenger/i) == "micromessenger") {
                                        var code = getUrlParam("code")
                                        $.ajax({
                                            type: "get",
                                                    //TODO:
                                            url: "/Api/Web/wxLogin",
                                            data: {code: code},
                                            dataType: "json",
                                            success: function(mag){
                                                if (mag.code == "success") {
                                                    if (mag.data.name == undefined || mag.data.name == null || mag.data.name == '') {
                                                        var username = ''
                                                    }else{
                                                        var username = mag.data.name
                                                    }

                                                    if (mag.data.deliveryAddress == undefined || mag.data.deliveryAddress == null || mag.data.deliveryAddress == '') {
                                                        var usermyaddress = ''
                                                        var usermyaddressid = ''
                                                    }else{
                                                        var usermyaddress = mag.data.deliveryAddress.address
                                                        var usermyaddressid = mag.data.deliveryAddress.id
                                                    }

                                                    if (mag.data.machine == undefined || mag.data.machine == null || mag.data.machine == '') {
                                                        var useraddressid = ''
                                                        var useraddressplace = ''
                                                    }else{
                                                        var useraddressid = mag.data.machine.id
                                                        var useraddressplace = mag.data.machine.place
                                                    }
                                                    if (mag.data.membership == undefined || mag.data.membership == null || mag.data.membership == '') {
                                                        var usermembershiplevel = 0
                                                        var usermembershipremaining = ''
                                                    }else{
                                                        var usermembershiplevel = mag.data.membership.level
                                                        var usermembershipremaining = mag.data.membership.remaining_amount
                                                    }
									                sessionStorage.setItem("sessionMembershiplevel", usermembershiplevel)
									                sessionStorage.setItem("sessionMembershipremaining", usermembershipremaining)
                                                    sessionStorage.setItem("sessionName", username)
                                                    sessionStorage.setItem("sessionAddress", usermyaddress)
                                                    sessionStorage.setItem("addressid", usermyaddressid)
                                                    sessionStorage.setItem("machineplaceId", useraddressid)
                                                    sessionStorage.setItem("machineplace", useraddressplace)
                                                    sessionStorage.setItem("sessionUserid", mag.data.id)
                                                    if (mag.data.wxResponseUserInfo == undefined || mag.data.wxResponseUserInfo == null || mag.data.wxResponseUserInfo == '') {
                                                        var userwxResponseUserInfo = ''
                                                    }else{
                                                        var userwxResponseUserInfo = mag.data.wxResponseUserInfo.headimgurl
                                                    }
                                                    sessionStorage.setItem("sessionuserWxResponseUserInfo", userwxResponseUserInfo)
                                                    window.location = "/static/webAPP/index.html";
                                                    
                                                }else{
                                                    layer.open({
                                                        content: mag.msg,
                                                        skin: 'msg',
                                                        time: 2 //2秒后自动关闭
                                                    });
                                                    window.location = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx45db0b3d36e1f241&redirect_uri=https%3A%2F%2Fwinterroad.club%2Fstatic%2FwebAPP%2Flogin.html&response_type=code&scope=snsapi_userinfo&#wechat_redirect";
                                                
                                                }
                                            },
                                            error: function(){
                                                layer.open({
                                                    content: '服务器错误，请退出重新登录',
                                                    skin: 'msg',
                                                    time: 2 //2秒后自动关闭
                                                });
                                            }
                                        })
                                    }
                                },
                                error: function(){
                                    sessionStorage.setItem("citycode", '127');
                                    sessionStorage.setItem("city", '合肥市');
                                    layer.open({
                                        content: '定位失败,默认为合肥市',
                                        skin: 'msg',
                                        time: 3 //2秒后自动关闭
                                    });
                                    window.location = "/static/webAPP/index.html";
                                }
                            })
                        },
                        error: function(){
                            layer.open({
                                content: '定位失败',
                                skin: 'msg',
                                time: 3 //2秒后自动关闭
                            });
                            if(userAgent.match(/MicroMessenger/i) == "micromessenger") {
                                var code = getUrlParam("code")
                                $.ajax({
                                    type: "get",
                                            //TODO:
                                    url: "/Api/Web/wxLogin",
                                    data: {code: code},
                                    dataType: "json",
                                    success: function(mag){
                                        if (mag.code == "success") {
                                            if (mag.data.name == undefined || mag.data.name == null || mag.data.name == '') {
                                                var username = ''
                                            }else{
                                                var username = mag.data.name
                                            }

                                            if (mag.data.deliveryAddress == undefined || mag.data.deliveryAddress == null || mag.data.deliveryAddress == '') {
                                                var usermyaddress = ''
                                                var usermyaddressid = ''
                                            }else{
                                                var usermyaddress = mag.data.deliveryAddress.address
                                                var usermyaddressid = mag.data.deliveryAddress.id
                                            }

                                            if (mag.data.machine == undefined || mag.data.machine == null || mag.data.machine == '') {
                                                var useraddressid = ''
                                                var useraddressplace = ''
                                            }else{
                                                var useraddressid = mag.data.machine.id
                                                var useraddressplace = mag.data.machine.place
                                            }
                                            if (mag.data.membership == undefined || mag.data.membership == null || mag.data.membership == '') {
                                                var usermembershiplevel = 0
                                                var usermembershipremaining = ''
                                            }else{
                                                var usermembershiplevel = mag.data.membership.level
                                                var usermembershipremaining = mag.data.membership.remaining_amount
                                            }
                                            sessionStorage.setItem("sessionMembershiplevel", usermembershiplevel)
                                            sessionStorage.setItem("sessionMembershipremaining", usermembershipremaining)

                                            sessionStorage.setItem("sessionName", username)
                                            sessionStorage.setItem("sessionAddress", usermyaddress)
                                            sessionStorage.setItem("addressid", usermyaddressid)

                                            sessionStorage.setItem("machineplaceId", useraddressid)
                                            sessionStorage.setItem("machineplace", useraddressplace)
                                            sessionStorage.setItem("sessionUserid", mag.data.id)
                                            if (mag.data.wxResponseUserInfo == undefined || mag.data.wxResponseUserInfo == null || mag.data.wxResponseUserInfo == '') {
                                                var userwxResponseUserInfo = ''
                                            }else{
                                                var userwxResponseUserInfo = mag.data.wxResponseUserInfo.headimgurl
                                            }
                                            sessionStorage.setItem("sessionuserWxResponseUserInfo", userwxResponseUserInfo)
                                            window.location = "/static/webAPP/index.html";
                                            
                                        }else{
                                            layer.open({
                                                content: mag.msg,
                                                skin: 'msg',
                                                time: 2 //2秒后自动关闭
                                            });
                                            window.location = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx45db0b3d36e1f241&redirect_uri=https%3A%2F%2Fwinterroad.club%2Fstatic%2FwebAPP%2Flogin.html&response_type=code&scope=snsapi_userinfo&#wechat_redirect";
                                        
                                        }
                                    },
                                    error: function(){
                                        layer.open({
                                            content: '服务器错误，请退出重新登录',
                                            skin: 'msg',
                                            time: 2 //2秒后自动关闭
                                        });
                                    }
                                })
                            }
                        }
                    });
                });
            })


            function getUrlParam(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
				var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
				var context = "";
				if (r != null){
					context = r[2];
					reg = null;
					r = null;
				}
				return context == null || context == "" || context == "undefined" ? "" : context;
			}
		</script>
	</body>
</html>
